﻿using AutoMapper;
using ManagementApi.Models;
using ManagementApi.Models.Payroll;
using ManagementApi.Models.System.Setting;
using Microsoft.AspNetCore.Mvc;
using Modules;
using Modules.Payroll;
using Modules.System.Settings;
using Modules.System.Settings.Entitys;
using PmSoft;

namespace ManagementApi.Controllers.SystemManage
{
    /// <summary>
    /// 主管单位相关业务接口
    /// </summary>
    [Route("/api/v1/setting")]
    [ApiController]
    public class SettingController : ControllerBase
    {
        private readonly IMapper mapper;
        private readonly WagePayrollSettingService wagePayrollSettingService;

        public SettingController(IMapper mapper, WagePayrollSettingService wagePayrollSettingService)
        {
            this.mapper = mapper;
            this.wagePayrollSettingService = wagePayrollSettingService;
        }

        #region 考勤代发状态设置

        /// <summary>
        /// 考勤设置
        /// </summary>
        /// <param name="form"></param>
        /// <returns></returns>
        [HttpPost("setCheckinWageSetting")]
        public ApiResult SetCheckinWageSetting(CheckinWageStatusSettingEditForm form)
        {
            CheckinWageStatusSettings checkinSettings = new();
            mapper.Map(form, checkinSettings);
            SettingManager<CheckinWageStatusSettings>.Instance.Save(checkinSettings);
            return ApiResult.Success("设置成功");
        }

        /// <summary>
        /// 考勤设置获取
        /// </summary>
        /// <returns></returns>
        [HttpGet("getCheckinWageSetting")]
        public ApiResult<CheckinWageStatusSettingDto> GetCheckinWageSetting()
        {
            CheckinWageStatusSettings checkinSettings = SettingManager<CheckinWageStatusSettings>.Instance.Get();
            CheckinWageStatusSettingDto checkinSettingsDto = new();
            mapper.Map(checkinSettings, checkinSettingsDto);
            return ApiResult<CheckinWageStatusSettingDto>.Success(checkinSettingsDto);
        }

        #endregion 考勤代发状态设置

        #region 短信发送内容设置

        /// <summary>
        /// 短信发送内容设置
        /// </summary>
        /// <param name="form"></param>
        /// <returns></returns>
        [HttpPost("setRetrievePasswordSetting")]
        public ApiResult SetRetrievePasswordSetting(RetrievePasswordSettingEditForm form)
        {
            RetrievePasswordSetting passwordSetting = new();
            mapper.Map(form, passwordSetting);
            SettingManager<RetrievePasswordSetting>.Instance.Save(passwordSetting);
            return ApiResult.Success("设置成功");
        }

        /// <summary>
        /// 短信发送内容获取
        /// </summary>
        /// <returns></returns>
        [HttpGet("getRetrievePasswordSetting")]
        public ApiResult<RetrievePasswordSettingDto> GetRetrievePasswordSetting()
        {
            RetrievePasswordSetting passwordSetting = SettingManager<RetrievePasswordSetting>.Instance.Get();
            RetrievePasswordSettingDto passwordSettingDto = new();
            mapper.Map(passwordSetting, passwordSettingDto);
            return ApiResult<RetrievePasswordSettingDto>.Success(passwordSettingDto);
        }

        #endregion 短信发送内容设置

        #region 工资单限额设置(按行业分类)

        /// <summary>
        /// 获取工资单限额设置
        /// </summary>
        /// <returns></returns>
        [HttpGet("getpayrollSetting")]
        public ApiResult<IEnumerable<WagePayrollSettingDto>> GetWagePayrollSettings()
        {
            return ApiResult<IEnumerable<WagePayrollSettingDto>>.Success(wagePayrollSettingService.GetAll().AsDtos());
        }

        /// <summary>
        /// 工资单限额设置
        /// </summary>
        /// <param name="forms"></param>
        /// <returns></returns>
        [HttpPost("setpayrollSetting")]
        public ApiResult SetWagePayrollSettings(IEnumerable<WagePayrollSettingEditForm> forms)
        {
            if (forms == null || !forms.Any())
                return ApiResult.Failure("没有传入任何数据");
            var editForms = forms.GroupBy(p => new { p.IndustryType, p.WorkType })
               .Where(g => g.Count() > 1)
               .SelectMany(g => g);
            string errMsg = "";
            bool isError = false;
            if (editForms.Any())
            {
                foreach (var item in editForms)
                {
                    errMsg += $"行业: {item.IndustryType?.GetDescription()}, 工种: {item.WorkType?.GetDescription()} 重复录入;";
                    isError = true;
                }
            }
            if (isError) return ApiResult.Failure(errMsg);
            List<WagePayrollSetting> wagePayrollSettings = new();
            mapper.Map(forms, wagePayrollSettings);
            wagePayrollSettingService.Save(wagePayrollSettings);
            return ApiResult.Success("设置成功");
        }

        #endregion 工资单限额设置

        #region 工资单限额设置（当月最大限额）

        /// <summary>
        /// 当月最大限额设置
        /// </summary>
        /// <param name="form"></param>
        /// <returns></returns>
        [HttpPost("setWageMonthAmountSettings")]
        public ApiResult SetWageMonthAmountSettings(WageMonthAmountSettingsEditForm form)
        {
            WageMonthAmountSettings data = new();
            mapper.Map(form, data);
            SettingManager<WageMonthAmountSettings>.Instance.Save(data);
            return ApiResult.Success("设置成功");
        }

        /// <summary>
        /// 当月最大限额设置获取
        /// </summary>
        /// <returns></returns>
        [HttpGet("getWageMonthAmountSettings")]
        public ApiResult<WageMonthAmountSettingsDto> GetWageMonthAmountSettings()
        {
            WageMonthAmountSettings data = SettingManager<WageMonthAmountSettings>.Instance.Get();
            WageMonthAmountSettingsDto dto = new();
            mapper.Map(data, dto);
            return ApiResult<WageMonthAmountSettingsDto>.Success(dto);
        }
        #endregion
    }
}